package com.atguigu.spzx.manager.utils;

import com.atguigu.spzx.model.entity.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:L枣
 * Date:2024/9/13
 */
//封装树形菜单数据
public class MenuHelper {

    //递归实现封装过程
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
        //sysMenuList：所有菜单数据
        //创建一个list集合，用于封装最终数据
        List<SysMenu> trees = new ArrayList<>();
        //遍历所有菜单数据
        for (SysMenu sysMenu : sysMenuList){
            //找到递归入口，第一层菜单
            //条件：父节点id为0
            if (sysMenu.getParentId().longValue()==0){
                trees.add(findChildren(sysMenu,sysMenuList));
            }
        }
        return trees;
    }

    //递归查找下层菜单
    private static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> sysMenuList) {

        sysMenu.setChildren(new ArrayList<>());
        //递归查找
        for (SysMenu menu : sysMenuList){
            if (menu.getParentId().longValue()==sysMenu.getId().longValue()){
                sysMenu.getChildren().add(findChildren(menu,sysMenuList));
            }
        }
        return sysMenu;
    }

}
